﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSGQuickTimeTracker.Properties;


namespace PSGQuickTimeTracker
{
	public partial class SetHotkey : Form
	{		

		public SetHotkey()
		{
			InitializeComponent();
		}


		private void SetHotkey_Load(object sender, EventArgs e)
		{
			string curMods = Settings.Default.Hotkey_Mods;
			if (curMods.Contains("Control"))
				modCtl.Checked = true;
			if (curMods.Contains("Alt"))
				modAlt.Checked = true;
			if (curMods.Contains("Shift"))
				modShift.Checked = true;
			hkKey.Text = Settings.Default.Hotkey_hkKey.Replace("D", "").Replace("NumPad", "NumPad ");
		}



		private void btnSubmit_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(hkKey.Text))					
			{
				MessageBox.Show(this, Properties.Settings.Default.Prompt_IncompleteEntry, Properties.Settings.Default.Prompt_Title,MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				hkKey.Focus();
				return;
			}
			else if (GetModifierString() == Settings.Default.Hotkey_Mods && hkKey.Text == Settings.Default.Hotkey_hkKey)
			{
				Close();
			}
			else
			{
				Settings.Default.Hotkey_Mods = GetModifierString();
				string tmp = hkKey.Text.Replace(" ", "");
				Settings.Default.Hotkey_hkKey = ((tmp.Substring(0, 1) == "F" || tmp.Substring(0, 1) == "N") ? "" : "D") + tmp;
				Settings.Default.Save();
				DialogResult = System.Windows.Forms.DialogResult.Yes;
				Close();
			}
		}



		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (GetModifierString() != Settings.Default.Hotkey_Mods
					|| hkKey.Text != Settings.Default.Hotkey_hkKey)
			{
				if (MessageBox.Show(this, Settings.Default.Prompt_LoseChanges, Settings.Default.Prompt_Title,
							MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes)
				{
					return;
				}
			}
			else
				Close();
		}


		/// <summary>
		/// Gets a string representing the modifiers selected. Can be used to
		/// compare against the Modifiers Keys property.
		/// </summary>
		private string GetModifierString()
		{
			string retVal = string.Empty;
			if (modShift.Checked)
				retVal = "Shift";
			if (modCtl.Checked)
				retVal += (!string.IsNullOrEmpty(retVal) ? ", " : "") + "Control";
			if (modAlt.Checked)
				retVal += (!string.IsNullOrEmpty(retVal) ? ", " : "") + "Alt";
			return retVal;
		}
				
		
	}
}
